package com.csii.modules.online.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csii.modules.online.auth.entity.OnlAuthRelation;
import com.csii.modules.online.auth.mapper.OnlAuthRelationMapper;
import com.csii.modules.online.auth.service.IOnlAuthRelationService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service("onlAuthRelationServiceImpl")
public class OnlAuthRelationServiceImpl extends ServiceImpl<OnlAuthRelationMapper, OnlAuthRelation> implements IOnlAuthRelationService {
	/**
	 *
	 * @param roleId
	 * @param cgformId
	 * @param type
	 * @param authIds
	 */
	public void saveRoleAuth(String roleId, String cgformId, int type, List<String> authIds) {
		LambdaQueryWrapper lambdaQueryWrapper = Wrappers.lambdaQuery(OnlAuthRelation.class).eq(OnlAuthRelation::getCgformId, cgformId).eq(OnlAuthRelation::getType, type)
				.eq(OnlAuthRelation::getRoleId, roleId);
		((OnlAuthRelationMapper) this.baseMapper).delete(lambdaQueryWrapper);
		ArrayList list = new ArrayList();
		Iterator it = authIds.iterator();

		while (it.hasNext()) {
			String id = (String) it.next();
			OnlAuthRelation relations = new OnlAuthRelation();
			relations.setAuthId(id);
			relations.setCgformId(cgformId);
			relations.setRoleId(roleId);
			relations.setType(type);
			list.add(relations);
		}

		this.saveBatch(list);
	}
}
